//
//  FolderMigration.swift
//  hello
//
//  Created by 赵康 on 2025/4/15.
//

import Fluent

extension Folder {
  struct Migration: AsyncMigration {
    var name: String { "FolderMigration" }
    
    func prepare(on database: any Database) async throws {
      try await database.schema(Folder.schema)
        .id()
        .field(Folder.FieldKeys.name, .string, .required)
        .field(Folder.FieldKeys.isDefault, .bool, .required, .custom("DEFAULT FALSE"))
        .field(Folder.FieldKeys.user, .uuid, .references(User.schema, .id))
        .field(Folder.FieldKeys.createdAt, .datetime, .required)
        .field(Folder.FieldKeys.updatedAt, .datetime, .required)
        .create()
    }
    
    func revert(on database: any Database) async throws {
      try await database.schema(Folder.schema).delete()
    }
  }

  struct AddPositionFieldMigration: AsyncMigration {
    var name: String { "AddPositionFieldToFolderMigration" }
    
    func prepare(on database: any Database) async throws {
      try await database.schema(Folder.schema)
        .field(Folder.FieldKeys.position, .int, .required, .custom("DEFAULT 0"))
        .update()
    }
    
    func revert(on database: any Database) async throws {
      try await database.schema(Folder.schema)
        .deleteField(Folder.FieldKeys.position)
        .update()
    }
  }
} 